
var post = $('#post'),
    postAdd = $('#post-add'),
    postEdit = $('#post-edit'),
    postAddName = $('#post-add-name'),
    postEditName = $('#post-edit-name'),
    postEditId = $('#post-edit-id'),
    postSearchKeywords = $('#post-search-keywords'),
    postSearchDateType = $('#post-search-date-type'),
    postSearchDateFrom = $('#post-search-date-from'),
    postSearchDateTo = $('#post-search-date-to'),
    postTool = $('#post-tool'),
    postDate,
    postOpt,
    postName;
post.datagrid({
    url: ThinkPHP['MODULE'] + '/Post/getList',
    fit: true,
    fitColumns: true,
    border: false,
    rownumbers: true,
    pagination: true,
    pageSize:10,
    pageNumber:1,
    pageList:[10,20,30,40,50],
    sortName:'create_time',
    sortOrder: 'DESC',
    toolbar: '#post-tool',
    columns: [[
        {
            field: 'id',
            title: '自动编号',
            width: 100,
            checkbox: true,
        },
        {
            field: 'name',
            title: '职位名称',
            width: 100,
        },
        {
            field: 'create_time',
            title: '创建时间',
            width: 100,
            sortable: true,

        }
    ]],
});

//工具条操作
postOpt = {
    add: function () {
        postAdd.dialog('open');
    },
    edit: function () {
        var rows = post.datagrid('getSelections');
        if (rows.length == 1) {
            postEdit.dialog('open');
            $.ajax({
                url: ThinkPHP['MODULE'] + '/Post/getOne',
                type: 'POST',
                data: {
                    'id': rows[0].id
                },
                beforeSend:function () {
                    $.messager.progress({'text':'正在获取数据...'});
                },
                success: function (data) {
                    $.messager.progress('close');
                    if (data) {
                        postEdit.form('load',{
                           'id' : data.id,
                           'name' :data.name
                        })
                        postEditName.textbox('textbox').select();
                    } else {
                        $.messager.alert('操作警告', '没有获取到相应数据', 'warning');
                    }
                }
            });
        } else {
            $.messager.alert('编辑操作', '编辑记录只能选择一条数据', 'warning');
        }
    },
    remove: function () {
        var rows = post.datagrid('getSelections');
        var ids = [];
        if (rows.length > 0) {
            for(var i=0; i<rows.length; i++){
                ids.push(rows[i].id);
            }

            $.messager.confirm('删除操作', '你真的要删除 '+ rows.length + '条数据吗？',function (flag) {
                if(flag) {
                    $.ajax({
                        url: ThinkPHP['MODULE'] +　'/Post/remove',
                        type: 'POST',
                        data: {
                            'ids' : ids.join(','),
                        },
                        beforeSend: function () {
                            post.datagrid('loading');
                        },
                        success: function (data) {
                            post.datagrid('loaded');
                            if (data) {
                                post.datagrid('reload');
                                $.messager.show({
                                    title: '删除提示',
                                    msg : '删除' + data + '条数据'
                                });
                            } else {
                                $.messager.alert('删除提示','没有任何数据被删除','warning');
                            }


                        }
                    });
                }
            });

        } else {
            $.messager.alert('编辑操作', '删除记录至少选择一条数据', 'warning');
        }

    },
    reload: function () {
        post.datagrid('reload');
    },
    redo: function () {
        post.datagrid('unselectAll');
    },
    search: function () {
        if (postTool.form('validate')) {
            post.datagrid('load', {
                keywords: postSearchKeywords.textbox('getValue'),
                dateType: postSearchDateType.textbox('getValue'),
                dateFrom: postSearchDateFrom.textbox('getValue'),
                dateTo: postSearchDateTo.textbox('getValue'),
            });
        } else {
            postSearchDateType.combobox('showPanel')
        }
    },
    reset: function () {
        postSearchKeywords.textbox('clear');
        postSearchDateType.combobox('clear').combobox('disableValidation');
        postSearchDateFrom.datebox('clear');
        postSearchDateTo.datebox('clear');
        post.datagrid('sort',{
            sortName: 'create_time',
            sortOrder: 'DESC'
        });
        this.search();
    }
}
/***********************新增-编辑********************/
//新增对话框
postAdd.dialog({
    title: '职位新增',
    width: 400,
    height:190,
    closed: true,
    iconCls:'icon-add',
    modal:true,
    maximizable:true,
    buttons:[{
        text:'保存',
        size:'large',
        iconCls:'icon-accept',
        handler:function(){
            if (postAdd.form('validate')) {
                $.ajax({
                    url: ThinkPHP['MODULE'] + '/Post/register',
                    type: 'POST',
                    data: {
                        'name': postAddName.val()
                    },
                    beforeSend: function () {
                        $.messager.progress({
                            'text': '正在处理中...'
                        });
                    },
                    success: function (data) {
                        $.messager.progress('close');
                        if (data > 0) {
                            $.messager.show({
                                title: '操作提示',
                                  msg: '添加成功'
                            });
                            postAdd.dialog('close');
                            post.datagrid('load');
                        } else if (data == -1) {
                            $.messager.alert('添加操作', '职位名称被占用', 'warning', function () {
                                postAddName.textbox('textbox').select();
                            });
                        }
                    }
                });
            }

        }
    },{
        text:'取消',
        size:'large',
        iconCls:'icon-cross',
        handler:function(){
            postAdd.dialog('close');
        }
    }],
    onClose: function () {
        postAdd.form('reset');
        postAdd.dialog('center');
    }



});

//编辑对话框
postEdit.dialog({
    title: '职位编辑',
    width: 400,
    height:190,
    closed: true,
    iconCls:'icon-edit',
    modal:true,
    maximizable:true,
    buttons:[{
        text:'保存',
        size:'large',
        iconCls:'icon-accept',
        handler:function(){
            if (postEdit.form('validate')) {
                $.ajax({
                    url: ThinkPHP['MODULE'] + '/Post/update',
                    type: 'POST',
                    data: {
                        'id': postEditId.val(),
                        'name': postEditName.val()
                    },
                    beforeSend: function () {
                        $.messager.progress({
                            'text': '正在处理中...'
                        });
                    },
                    success: function (data) {
                        $.messager.progress('close');
                        if (data > 0) {
                            $.messager.show({
                                title: '操作提示',
                                msg: '修改成功'
                            });
                            postEdit.dialog('close');
                            post.datagrid('reload');
                        } else if (data == -1) {
                            $.messager.alert('修改操作', '职位名称被占用', 'warning', function () {
                                postEditName.textbox('textbox').select();
                            });
                        } else if(data == 0) {
                            $.messager.alert('修改操作', '尚未修改字段或未知错误', 'warning', function () {
                                postEditName.textbox('textbox').select();
                            });
                        }
                    }
                });
            }
        }
    },{
        text:'取消',
        size:'large',
        iconCls:'icon-cross',
        handler:function(){
            postEdit.dialog('close');
        }
    }],
    onClose: function () {
        postEdit.form('reset');
        postEdit.dialog('center');
    }



});

/** 表单字段区域 **/
postName = {
    width:220,
    height:32,
    required:true,
    //validType:'length[2,20]',
    missingMessage:'请输入职位名称',
    invalidMessage:'职位名称2到20位'
}

//新增职位
postAddName.textbox(postName);

//编辑职位
postEditName.textbox(postName);
/***********************新增-编辑********************/



/***********************查询区域********************/
postSearchKeywords.textbox({
    width: 150,
    prompt: '职位'
});

//时间类型
postSearchDateType.combobox({
    width: 100,
    editable: false,
    prompt: '时间类型',
    data: [{
       id: 'create_time',
       text: '创建时间'
    }],
    valueField: 'id',
    textField: 'text',
    required: true,
    novalidate: true,
    panelHeight: 'auto',
    tipPosition : 'left',
    missingMessage : '请选择时间类型'
})

//查询时间对象
postDate = {
    width: 100,
    editable: false,
    onSelect:function () {
        if (postSearchDateType.combobox('enableValidation').combobox('isValid') == false) {
            postSearchDateType.combobox('showPanel');
        }
    }

}
postDate.prompt = '起始时间';
postSearchDateFrom.datebox(postDate);
postDate.prompt = '结束时间'
postSearchDateTo.datebox(postDate);
/***********************查询区域********************/